package com.lihh.rocket5.normal;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class SyncProducer001Consumer {
  public static void main(String[] args) throws MQClientException {
    // 构建 消费者实例
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer-111");
    // 设置 name server
    consumer.setNamesrvAddr("127.0.0.1:9876");
    // 订阅主题
    consumer.subscribe("topic111", "*");
    
    // *** 可以通过 API 设置重试次数
    consumer.setMaxReconsumeTimes(2);
    
    // 设置 偏移量
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    
    // 添加监听器
    consumer.registerMessageListener(new MessageListenerConcurrently() {
      @Override
      public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> messageExts, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        
        for (MessageExt msg : messageExts) {
          System.out.println(msg);
          msg.setDelayTimeLevel(1);
          return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
      }
    });
    
    consumer.start();
    System.out.println("开始消费 ...");
    
    while (true) {
    }
  }
}
